<?php
// +----------------------------------------------------------------------
// | Tplay [ WE ONLY DO WHAT IS NECESSARY ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017 http://tplay.pengyichen.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 听雨 < 389625819@qq.com >
// +----------------------------------------------------------------------


namespace app\admin\controller;

use \think\Db;
use \think\Cookie;
use app\admin\controller\Permissions;
use app\admin\model\AccountModel;

class Staticbonus extends Permissions
{
    public function bonus()
    {

        if (request()->isAjax()) {
            $param = input('param.');
            $where = [];
            $userWhere = [];
            if (isset($param['phone']) && !empty($param['phone'])) {
                $userWhere['phone'] = ['like', '%' . $param['phone'] . '%'];
                $userId= db('users')->where($userWhere)->column('id');
                if (!empty($userId)) {
                    $where['uid'] = ['in',$userId];
                }
            }
            if (isset($param['type']) && !empty($param['type'])) {
                $where['type'] = $param['type'];
            }            

            if (isset($param['start']) && !empty($param['start'])) {

                if (!empty($param['end'])) {
                    $and_where['created_at'] = ['between', [strtotime($param['start']),strtotime($param['end'])]];
                }else{
                    $param['start'] = strtotime($param['start']);
                    $where['created_at'] = ['>=', $param['start']];
                }
            }else{
                if (isset($param['end']) && !empty($param['end'])) {
                    $param['end'] = strtotime($param['end'] . ' 23:59:59');
                    $and_where['created_at'] = ['<=', $param['end']];
                }
            }
            $model = new AccountModel();
            $field = "id,uid,money,balance,message,type,class,status,create_time";
            $data = $model->getUsersByWhere($param['page'], $param['limit'], $where, $field);
            $all_user = [];
            foreach ($data as $k => $v) {
                $data[$k]['nickname'] = $v->users->nickname;
                $data[$k]['phone'] = $v->users->phone;
                $data[$k]['add_date'] = $v['create_time'];
                unset($v->users);
                $data[$k]['class'] = $v['class'] == 1 ? "增加" : "减少";
                $data[$k]['type'] = AccountModel::TYPE[$v['type']];
                // $data[$k]['status'] = "<a onclick='lock(" . $v->id . "," . $v->status . ")' class='user_status_" . $v->id . "'>" . $user_status . "</a>";
                // $operation = set_operation($button);
                // $data[$k]['operation'] = $operation;
            }
            $count = $model->getAllUsers($where);
            return json(["code" => 0, "count" => $count, "data" => $data]);
        }
        $this->assign('type', AccountModel::TYPE);
        return $this->fetch('index');
    }
}
